package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanApprovalPreviewVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanCoeffcienDifferenceDetailVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanCoeffcienDifferenceVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanStageCoeffcienDifferenceVo;
import cn.com.cifi.mars.constant.BigStandardFormatsEnum;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.StandardFormatsEnum;
import cn.com.cifi.mars.constant.StandardFormatsMapEnum;
import cn.com.cifi.mars.entity.*;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.mapper.mars.*;
import cn.com.cifi.mars.mapper.wrapper.ReturnPlanVersionCoefficientWrapper;
import cn.com.cifi.mars.service.DataRecoveryService;
import cn.com.cifi.mars.service.ReturnPlanVersionCoefficientService;
import cn.com.cifi.mars.util.UUIDUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: JingYuanXiang
 * @date: 2021/4/7 15:44
 */
@Slf4j
@Service
public class DataRecoveryServiceImpl implements DataRecoveryService {

    @Autowired
    private ReturnPlanVersionMapper returnPlanVersionMapper;
    @Autowired
    private StageMapper stageMapper;
    @Autowired
    private ApprovalBillMapper approvalBillMapper;
    @Autowired
    private ApprovalPreviewMapper approvalPreviewMapper;
    @Autowired
    private ReturnCoefficientDetailMapper returnCoefficientDetailMapper;
    @Autowired
    private ReturnPlanVersionCoefficientWrapper returnPlanVersionCoefficientWrapper;
    @Autowired
    private ReturnPlanVersionCoefficientMapper returnPlanVersionCoefficientMapper;
    @Autowired
    private ProjectReturnCoefficientMapper projectReturnCoefficientMapper;
    @Autowired
    private ApprovalBillDetailMapper approvalBillDetailMapper;

    @Override
    public void recoveryOne(IdmUser user) {
        QueryWrapper<ApprovalBill> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("business_type","return_coefficient").in("state", Arrays.asList("approval_success","approval"));
        List<ApprovalBill> approvalBillList = approvalBillMapper.selectList(queryWrapper1);

        List<String> billIdList = approvalBillList.stream().map(s -> s.getId()).collect(Collectors.toList());
        QueryWrapper<ApprovalPreview> queryWrapper2 = new QueryWrapper();
        queryWrapper2.in("bill_id",billIdList);
        List<ApprovalPreview> approvalPreviewList = approvalPreviewMapper.selectList(queryWrapper2);

        List<String> idOneList = new ArrayList<>();
        List<String> idTwoList = new ArrayList<>();
        List<ReturnCoefficientDetail> detailList = new ArrayList<>();

        for (ApprovalPreview approvalPreview : approvalPreviewList){
            String content = approvalPreview.getContent();
            JSONObject object1 = JSONObject.parseObject(content);
            String coefficientId = object1.getString("coefficientId");

            QueryWrapper<ReturnCoefficientDetail> q2 = new QueryWrapper();
            q2.eq("coefficient_id",coefficientId)
                    .eq("is_delete",IsDeleteEnum.NO.getKey());
            List<ReturnCoefficientDetail> returnCoefficientDetailList = returnCoefficientDetailMapper.selectList(q2);

            if(CollectionUtils.isNotEmpty(returnCoefficientDetailList)){
                continue;
            }


            JSONObject object2 = object1.getJSONObject("vsCityMap");
            if(object2 == null){
                object2 = object1.getJSONObject("vsRealityMap");
            }
            if(object2 == null){
                object2 = object1.getJSONObject("vsCityMap");
            }
            if(object2 == null){
                idTwoList.add(coefficientId);
                continue;
            }
            JSONObject object3 = object2.getJSONObject("adjustment");
            for (Map.Entry<String, Object> entry : object3.entrySet()) {
                String proProductCode = entry.getKey();
                JSONObject object4 = JSONObject.parseObject(entry.getValue().toString());
                ReturnCoefficientDetail detail = new ReturnCoefficientDetail();
                detail.setEightMonth(object4.get("eightMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("eightMonth").toString()));
                detail.setEighteenMonth(object4.get("eighteenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("eighteenMonth").toString()));
                detail.setElevenMonth(object4.get("elevenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("elevenMonth").toString()));
                detail.setFifteenMonth(object4.get("fifteenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("fifteenMonth").toString()));
                detail.setFiveMonth(object4.get("fiveMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("fiveMonth").toString()));
                detail.setFourMonth(object4.get("fourMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("fourMonth").toString()));
                detail.setFourteenMonth(object4.get("fourteenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("fourteenMonth").toString()));
                detail.setNineMonth(object4.get("nineMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("nineMonth").toString()));
                detail.setNineteenMonth(object4.get("nineteenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("nineteenMonth").toString()));
                detail.setOneMonth(object4.get("oneMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("oneMonth").toString()));
                detail.setSevenMonth(object4.get("sevenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("sevenMonth").toString()));
                detail.setSeventeenMonth(object4.get("seventeenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("seventeenMonth").toString()));
                detail.setSixMonth(object4.get("sixMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("sixMonth").toString()));
                detail.setSixteenMonth(object4.get("sixteenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("sixteenMonth").toString()));
                detail.setTenMonth(object4.get("tenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("tenMonth").toString()));
                detail.setThirteenMonth(object4.get("thirteenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirteenMonth").toString()));
                detail.setThirtyFiveMonth(object4.get("thirtyFiveMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtyFiveMonth").toString()));
                detail.setThirtyFourMonth(object4.get("thirtyFourMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtyFourMonth").toString()));
                detail.setThirtyMonth(object4.get("thirtyMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtyMonth").toString()));
                detail.setThirtyOneMonth(object4.get("thirtyOneMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtyOneMonth").toString()));
                detail.setThirtySixMonth(object4.get("thirtySixMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtySixMonth").toString()));
                detail.setThirtyThreeMonth(object4.get("thirtyThreeMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtyThreeMonth").toString()));
                detail.setThirtyTwoMonth(object4.get("thirtyTwoMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("thirtyTwoMonth").toString()));
                detail.setThreeMonth(object4.get("threeMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("threeMonth").toString()));
                detail.setTwelveMonth(object4.get("twelveMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twelveMonth").toString()));
                detail.setTwentyEightMonth(object4.get("twentyEightMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyEightMonth").toString()));
                detail.setTwentyFiveMonth(object4.get("twentyFiveMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyFiveMonth").toString()));
                detail.setTwentyFourMonth(object4.get("twentyFourMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyFourMonth").toString()));
                detail.setTwentyMonth(object4.get("twentyMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyMonth").toString()));
                detail.setTwentyNineMonth(object4.get("twentyNineMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyNineMonth").toString()));
                detail.setTwentyOneMonth(object4.get("twentyOneMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyOneMonth").toString()));
                detail.setTwentySevenMonth(object4.get("twentySevenMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentySevenMonth").toString()));
                detail.setTwentySixMonth(object4.get("twentySixMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentySixMonth").toString()));
                detail.setTwentyThreeMonth(object4.get("twentyThreeMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyThreeMonth").toString()));
                detail.setTwentyTwoMonth(object4.get("twentyTwoMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twentyTwoMonth").toString()));
                detail.setTwoMonth(object4.get("twoMonth") == null ? BigDecimal.ZERO : new BigDecimal(object4.get("twoMonth").toString()));
                detail.setProProductCode(proProductCode);
                detail.setProProductType(StandardFormatsEnum.getValueByKey(proProductCode));
                detail.setThreeProductCode(StandardFormatsMapEnum.getValueByKey(proProductCode));
                detail.setThreeProductName(BigStandardFormatsEnum.getValueByKey(detail.getThreeProductCode()));
                detail.setId(UUIDUtils.create());
                detail.setCoefficientId(coefficientId);
                detail.setCreateBy(user.getUsername());
                detail.setCreateTime(LocalDateTime.now());
                detail.setUpdateBy(user.getUsername());
                detail.setUpdateTime(LocalDateTime.now());
                detail.setIsDelete(IsDeleteEnum.NO.getKey());
                returnCoefficientDetailMapper.insert(detail);
                idOneList.add(coefficientId);
                detailList.add(detail);
            }
        }
        log.info(idOneList.toString());
        log.info(idTwoList.toString());
    }

    @Override
    public void recoveryTwo(IdmUser user, List<String> idList) {
        for (String id : idList){
            ProjectReturnCoefficient returnCoefficient = projectReturnCoefficientMapper.selectById(id);
            QueryWrapper<ProjectReturnCoefficient> q1 = new QueryWrapper();
            q1.eq("project_id",returnCoefficient.getProjectId())
                    .eq("version_type",returnCoefficient.getVersionType())
                    .eq("is_delete",IsDeleteEnum.NO.getKey())
                    .orderByDesc("update_time")
                    .last("limit 1");
            ProjectReturnCoefficient returnCoefficient1 = projectReturnCoefficientMapper.selectOne(q1);
            if(returnCoefficient.getId().equals(returnCoefficient1.getId())){
                log.info("变更失败》》》》》》" + returnCoefficient.getProjectId()+ " : "+ returnCoefficient.getVersionType()+ " : "+ id);
                continue;
            }


            QueryWrapper<ReturnCoefficientDetail> q2 = new QueryWrapper();
            q2.eq("coefficient_id",returnCoefficient1.getId())
                    .eq("is_delete",IsDeleteEnum.NO.getKey());
            List<ReturnCoefficientDetail> returnCoefficientDetailList = returnCoefficientDetailMapper.selectList(q2);

            returnCoefficientDetailList.forEach(s->s.setCoefficientId(id));
            for(ReturnCoefficientDetail detail: returnCoefficientDetailList){
                returnCoefficientDetailMapper.updateById(detail);
            }
            projectReturnCoefficientMapper.deleteById(returnCoefficient1);
        }
    }

    @Autowired
    private ReturnPlanVersionCoefficientService returnPlanVersionCoefficientService;

    @Override
    public void recoveryThree(IdmUser user, List<String> idList) {
        for (String id : idList){
            ReturnPlanVersion returnPlanVersion = returnPlanVersionMapper.selectById(id);
            List<ReturnPlanVersionCoefficient> returnPlanVersionCoefficientList = returnPlanVersionCoefficientWrapper.selectListByReturnPlanVersionId(id, IsDeleteEnum.NO.getKey());
            for (ReturnPlanVersionCoefficient returnPlanVersionCoefficient :returnPlanVersionCoefficientList ){
                returnPlanVersionCoefficient.setIsDelete(IsDeleteEnum.YES.getKey());
                returnPlanVersionCoefficientMapper.updateById(returnPlanVersionCoefficient);
            }
            returnPlanVersionCoefficientService.createList(returnPlanVersion,user);
        }
    }

}
